import GParam from '../../../Core/GEvent/GParam';
import GChild from "../../../Core/GView/GChild";
import { VIEW_ID } from '../../Common/UI';
import { Res } from '../../Common/UIResources';
import GameMgr from '../../Logic/GameMgr';
import { GCtrl } from './../../../Core/GCtrl';
import { CMsg, SQUAD_BUTTON } from './../../Common/Define';

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Fight/JXResultHero')

export default class JXResultHero extends GChild {

	@property({ type: cc.Sprite, tooltip: "英雄品质背景" }) heroTarit: cc.Sprite = null
	@property({ type: cc.Sprite, tooltip: "英雄头像" }) icon: cc.Sprite = null;
	@property({ type: cc.Node, tooltip: "皇冠" }) king: cc.Node = null;
	@property({ type: cc.Node, tooltip: "升级变强" }) up: cc.Node = null;
	@property(cc.Label) lv: cc.Label = null;
	@property(cc.Label) exp: cc.Label = null;
	@property(cc.Sprite) expBar: cc.Sprite = null;

	protected heroId: number = null;
	private _cb: Function = null;
	/**关闭经验条 */
	public closeExp() {
		this.exp.node.active = false;
		this.expBar.node.parent.active = false;
	}

	/**开启通道 */
	public openUp() {
		this.up.active = true;
	}

	public setView(heroId: number, addExp: number, cb?: Function) {
		this._cb = cb;
		let hero = GameMgr.rHeroData.getHero(heroId);
		if (!hero) {
			this.node.removeFromParent();
			this.node.destroy()
			return;
		}
		this.heroId = heroId;
		this.assetImpl.spriteAtlasFrame(this.heroTarit, Res.texture.views.common, "hero_" + hero.trait)
		this.assetImpl.spriteFrame(this.icon, Res.texture.hero.hero + '' + hero.tableId);
		let nextData = GameMgr.lvData.getRaw<SLvDataRaw>(hero.lv + 1);
		this.lv.string = "Lv" + hero.lv;
		if (nextData) {
			let curPro = hero.exp / nextData.exp;
			// this.exp.string = LNumber(hero.exp + addExp) + "/" + LNumber(nextData.exp);
			this.exp.string = '经验+' + addExp;
			this.expBar.fillRange = curPro;
			let nextPro = (hero.exp + addExp) / nextData.exp;
			cc.tween(this.expBar).to(0.2, { fillRange: nextPro }).start();
			let info: HeroAddExp = {
				heroId: hero.tableId,
				exp: addExp
			}
			GCtrl.ES.emit(CMsg.client.hero.onHeroAddExp, new GParam(info))
		} else {
			this.exp.string = "MAX"
			this.expBar.fillRange = 1;
		}
	}

	public showKing() {
		this.king.active = true;
	}

	/**跳转英雄详情页 */
	public jumpHeroDetailCtrl() {
		let rHeroData = null;
		let allHero = GameMgr.rHeroData.getHaveHeros(SQUAD_BUTTON.ALL);
		allHero.have.forEach((v) => {
			// console.log("v", v);
			if (v.tableId == this.heroId) {
				rHeroData = v;
			}
		})
		let SHeroData = GameMgr.heroData.getRaw<SHeroDataRaw>(this.heroId);
		if (this._cb) {
			this._cb();
		}
		GCtrl.ES.emit(CMsg.client.home.onaddLevel);
		GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.detailsCtrl, rHeroData, SHeroData);
	}
}